home *** CD-ROM | disk | FTP | other *** search
Wrap
-- card: 22649 from stack: in.3r -- bmap block id: 0 -- flags: 0000 -- background id: 2202 -- name: InsertInList -- part contents for background part 10 ----- text ----- 12 -- part contents for background part 19 ----- text ----- Functions -- part contents for background part 3 ----- text ----- InsertInList -- part contents for background part 2 ----- text ----- If you need to insert entries into an alphabetical list and need to know which line the new entry went into (such as if you needed to enter data that would be on the same line as the new entry but in another field) then you may find this function useful. InsertInList Function requires two parameters, a list and a new entry. It returns the line number that the new entry would be BEFORE alphabetically in the list. You would have to put the new entry and return before line (number returned) of the list. The function can be used like this: put InsertInList(newEntry,theList) into lineNum As this is written in HyperTalk it is not lightning fast, but it is sufficient. The function on the next card "FUNCTION ABC" is a little faster, but it needs the new entry and the list to be in all caps. copyright ©1988 - by Scott McGilliard all rights reserved ----------------------------------------------------------------------- -- copyright ©1988 - by Scott McGilliard - all rights reserved -- -- -- -- if you like this function and find it useful please send $5 to: -- -- Scott McGilliard -- -- 1515 Arborview -- -- Ann Arbor MI 48103 -- -- -- -- if you use this function please keep this notice with it -- ----------------------------------------------------------------------- --mouseUp handler uses the variable it for the new entry and-- --the field "list" for the list in calling function abc in place-- --of the line number in the put command-- on mouseUp ask "New entry?" if it = empty then exit mouseUp set cursor to 4 put it & return before line abc(it,field list) of field "list" end mouseUp --function abc starts here-- function abc newEntry,theList --if the list is empty then the obvious line number is returned-- if theList = empty then return(1) --sets up for the sort-- --Tmax and Tmin will store the original top and bottom of the list-- --while maxNum and minNum will change as the sort continues-- --gap is the number half way between the maximum and minimum of-- --the current segment being checked lineNum is the current line of-- --the list being checked - by checking the middle line of the list-- --the posibilities are cut in half with each comparison-- --with a 100 word list the maximum number of comparisons would-- --be 7 and with 200 words only 8 it's not quite that simple but-- --that's the concept behind this function-- put the number of lines of theList into maxNum put maxNum into TMax put 1 into minNum put minNum into TMin put maxNum div 2 into gap if gap = 0 then put 1 into gap put gap into lineNum put 1 into whichChar --start the sort-- repeat --uses another function to put the lower case ASCII number of the-- --two characters to be compared into variables to save time by-- --not having to convert each time they're compared-- put toLow(char whichChar of newEntry) into newNum put toLow(char whichChar of line lineNum of theList) into listNum --checks if entry belongs after this line of the list-- if newNum > listNum then --if it's between current line and the next or if it's-- --the last line of the list or if list can't be narrowed any-- --farther the next line is returned-- if newNum < toLow(char whichChar of line lineNum + 1¬ of theList) or lineNum = Tmax or maxNum = minNum then return(lineNum + 1) else --set up new minNum and gap for the next comparison-- put lineNum into minNum put gap div 2 into gap if gap = 0 then put 1 into gap put minNum + gap into lineNum end if else --checks if entry belongs before this line of the list-- if newNum < listNum then --if it's between current line and the previous one-- --or if it's the first line of the list then the current-- --line is returned-- if newNum > toLow(char whichChar of line lineNum - 1¬ of theList) or lineNum = Tmin then return(lineNum) else --set up new maxNum and gap for the next comparison-- put lineNum into maxNum put gap div 2 into gap if gap = 0 then put 1 into gap put maxNum - gap into lineNum end if else --set up new maxNum and minNum-- repeat with i = lineNum down to minNum if newNum ≠ toLow(char whichChar of line i of theList) then put i + 1 into minNum exit repeat end if end repeat repeat with i = lineNum to maxNum if newNum ≠ toLow(char whichChar of line i of theList) then put i - 1 into maxNum exit repeat end if end repeat --if there is no character to compare then the new entry-- --matches all the characters of the current line and is-- --shorter than the current line so return the new minNum-- add 1 to whichChar if char whichChar of newEntry = empty then return(minNum) --set up a new TMax and TMin to have the sort deal with only-- --the part of the list that starts with matching characters-- put maxNum into TMax put minNum into TMin --set up a new gap and lineNum to compare-- put (maxNum - minNum) div 2 into gap if gap = 0 then put 1 into gap if maxNum > minNum then put minNum + gap into lineNum else put minNum into lineNum end if end if end repeat end abc --function to convert the ASCII number of a character to the-- --lower case ASCII number-- function toLow az if the charToNum of az >= 65 and the charToNum¬ of az <= 90 then return(the charToNum of az + 32) else return(the charToNum of az) end toLow